package com.lili.backtrack;

import java.util.*;

/**
 * @Auther: 李 力
 * @Date: 2024/8/20
 * @Description: 组合
 * @version: 1.0
 */
public class LeetCode77 {

    public static List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> result = new ArrayList<>();
        dfs(1, n, k, new LinkedList<>(), result);
        return result;
    }

    private static void dfs(int start, int n, int k,
                            LinkedList<Integer> stack, List<List<Integer>> result) {
        if (stack.size() == k) {
            result.add(new ArrayList<>(stack));
            return;
        }
        for (int i = start; i <= n; i++) {
            if (k - stack.size() > n - i + 1) {
                continue;
            }
            stack.push(i);
            dfs(i + 1, n, k, stack, result);
            stack.pop();
        }
    }

}
